package com.mall.huohuo.service.member;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mall.huohuo.datasource.entity.TblMemberAddress;
import com.mall.huohuo.datasource.mapper.TblMemberAddressMapper;
import com.mall.huohuo.service.address.domain.MemberAddress;
import com.mall.huohuo.service.address.service.MemberAddressOperateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author zyconding
 * @version AuthParam.java, v 0.1 2020-08-07 16:23 zyconding
 */
@Slf4j
@Service
public class MemberAddressOperateServiceImpl implements MemberAddressOperateService {

    @Autowired
    private TblMemberAddressMapper tblMemberAddressMapper;


    /**
     * 更新收货地址
     * 设置默认 ， 删除地址。
     *
     * @param memberId      用户id
     * @param memberAddress 收货地址
     * @return 更新结果
     */
    @Override
    public Boolean updateMemberAddress(Long memberId, MemberAddress memberAddress) {
        Boolean isDefault = memberAddress.getIsDefault();
        TblMemberAddress tblMemberAddress = new TblMemberAddress();

        tblMemberAddress.setUpdateTime(LocalDateTime.now());
        if (Objects.nonNull(memberAddress.getIsDeleted()) && memberAddress.getIsDeleted()) {
            tblMemberAddress.setIsDeleted(true);
        }else{
            tblMemberAddress.setReceiveName(memberAddress.getReceiveName())
                    .setReceiveMobile(memberAddress.getReceiveMobile())
                    .setProvince(memberAddress.getProvince())
                    .setCity(memberAddress.getCity())
                    .setArea(memberAddress.getArea())
                    .setAddress(memberAddress.getAddress())
                    .setIsDefault(isDefault);
        }
        if (Objects.nonNull(isDefault) && isDefault) {
            //更新所有的为非默认
            updateUnDefault(memberId);
        }
        int update = tblMemberAddressMapper.update(tblMemberAddress, Wrappers.<TblMemberAddress>lambdaUpdate()
                .eq(TblMemberAddress::getMemberId, memberId)
                .eq(TblMemberAddress::getId, memberAddress.getId())
                .eq(TblMemberAddress::getIsDeleted, false)
        );
        return update == 1;
    }

    /**
     * 新增收货地址
     *
     * @param memberAddress 收货地址
     * @return 新增的结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveMemberAddress(Long memberId,MemberAddress memberAddress) {
        TblMemberAddress tblMemberAddress = new TblMemberAddress();

        Boolean isDefault = memberAddress.getIsDefault();
        if (isDefault) {
            //更新所有的为非默认
            updateUnDefault(memberId);
        }
        tblMemberAddress.setReceiveName(memberAddress.getReceiveName())
        .setReceiveMobile(memberAddress.getReceiveMobile())
        .setProvince(memberAddress.getProvince())
        .setCity(memberAddress.getCity())
        .setArea(memberAddress.getArea())
        .setAddress(memberAddress.getAddress())
        .setIsDefault(isDefault)
        .setIsDeleted(false)
        .setCreateTime(LocalDateTime.now())
        .setUpdateTime(LocalDateTime.now())
        .setMemberId(memberId);
        int insert = tblMemberAddressMapper.insert(tblMemberAddress);


        return insert == 1;
    }

    /**
     * 将所有的都修改为非默认状态。
     * @param memberId
     * @return
     */
    private int updateUnDefault(Long memberId ){
        int update = tblMemberAddressMapper.update(
                new TblMemberAddress().setIsDefault(false).setUpdateTime(LocalDateTime.now()), Wrappers.<TblMemberAddress>lambdaUpdate()
                        .eq(TblMemberAddress::getMemberId, memberId)
                        .eq(TblMemberAddress::getIsDeleted, false));
        log.info("update all address to is_default =false ，memberId:{} result:{}",memberId,update);
        return update;
    }
}
